package marksys.free.com.marksys.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.io.File;
import java.util.List;

import marksys.free.com.marksys.R;

import static android.support.constraint.Constraints.TAG;

public class CardPagerAdapter extends PagerAdapter {
    private List<PhotoEntity> mData;
    private Context context;
    private CallbackDeleteInfo callbackDeleteInfo;

    public interface onDelListener {
        void onDel(int position);
    }

    private onDelListener listener;

    public void setDelListener(onDelListener listener) {
        this.listener = listener;
    }

    public CardPagerAdapter(Context context, List<PhotoEntity> list) {
        this.context = context;
        this.mData = list;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public boolean isViewFromObject(View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(@NonNull final ViewGroup container, int position) {
        View view = LayoutInflater.from(container.getContext())
                .inflate(R.layout.item_coverflow, container, false);
        container.addView(view);
        view.setTag(position);
        ImageView iv_img = view.findViewById(R.id.image);
        ImageView delete_img = view.findViewById(R.id.delete);

        Log.e(TAG, "instantiateItem: " + position + ":::" + mData.get(position).path);
        File file = new File(mData.get(position).path);
        Glide.with(context).load(file).into(iv_img);
        delete_img.setOnClickListener(view1 -> new AlertDialog.Builder(context)
                .setTitle("提示")
                .setMessage("是否删除该照片？")
                .setPositiveButton("确定", (dialog, which) -> {
//                            mData.remove(select);
//                            notifyDataSetChanged();
                    if (listener != null)
                        listener.onDel(position);
                })
                .setNegativeButton("取消", null)
                .show());

        return view;
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        // 最简单解决 notifyDataSetChanged() 页面不刷新问题的方法
        return POSITION_NONE;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
//        int select = position < mData.size() ? position : (position % mData.size());

    }

    public CallbackDeleteInfo getCallbackDeleteInfo() {
        return callbackDeleteInfo;
    }

    public void setCallbackDeleteInfo(CallbackDeleteInfo callbackDeleteInfo) {
        this.callbackDeleteInfo = callbackDeleteInfo;
    }

}
